package com.mfc.education.service.edu.service.impl;


import com.mfc.education.service.edu.entity.Chapter;
import com.mfc.education.service.edu.mapper.ChapterMapper;
import com.mfc.education.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mfc.education.service.edu.entity.Chapter;
import com.mfc.education.service.edu.entity.Video;
import com.mfc.education.service.edu.entity.vo.ChapterVo;
import com.mfc.education.service.edu.entity.vo.VideoVo;
import com.mfc.education.service.edu.feign.VodMediaService;
import com.mfc.education.service.edu.mapper.ChapterMapper;
import com.mfc.education.service.edu.mapper.VideoMapper;
import com.mfc.education.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {



    @Autowired
    private ChapterMapper ChapterMapper;

    @Autowired
    private VideoMapper videoMapper;




    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeChapterById(String id) {

        // 根据chapter_id删除Video(课时)
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id", id);
        videoMapper.delete(videoQueryWrapper);


        //删除章节
        return this.removeById(id);
    }

    @Override
    public List<ChapterVo> nestedList(String courseId) {

        // 方案2：效率高  1+1 个sql   有个冗余字段
        // 通过course_id 获取章节列表信息： List<Chapter> sql
        // 通过course_id 获取视频列表信息； List<Video> sql


        // 1、通过course_id 获取章节列表信息

        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByAsc("sort", "id");
        List<Chapter> chapterList = ChapterMapper.selectList(chapterQueryWrapper);


        // 2、通过course_id 获取视频列表信息
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoQueryWrapper.orderByAsc("sort", "id");
        List<Video> videoList = videoMapper.selectList(videoQueryWrapper);


        // 3、讲视频归类到章节中
        List<ChapterVo> chapterVoList = new ArrayList<>();

        for (Chapter chapter : chapterList) {
            //组装chapterVoList
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVoList.add(chapterVo);

            //组装videoVoList
            String chapterVoId = chapterVo.getId();
            List<VideoVo> videoVoList = new ArrayList<>();
            for (Video video : videoList) {
                if (chapterVoId.equals(video.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }


        return chapterVoList;


        //// 组装章节列表:List<ChapterVo>
        //// 方案1:效率低 1+n个sql
        //// 获取章节列表信息:List<Chapter> sql
        // 遍历List<Chapter>{
        //  通过chapter_id查询List<Video> sql
        // }

//        List<ChapterVo> list = new ArrayList<>();
//        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("course_id", courseId);
//        List<Chapter> chapterList = ChapterMapper.selectList(queryWrapper);
//        for (Chapter chapter : chapterList) {
//            ChapterVo chapterVo = new ChapterVo();
//            if (chapter != null) {
//                BeanUtils.copyProperties(chapter, chapterVo);
//                QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
//                videoQueryWrapper.eq("chapter_id", chapter.getCourseId());
//                List<Video> videoList = videoMapper.selectList(videoQueryWrapper);
//                 String chapterVoId = chapterVo.getId();
//                            List<VideoVo> videoVoList = new ArrayList<>();
//                            for (Video video : videoList) {
//                                if (chapterVoId.equals(video.getChapterId())) {
//                                    VideoVo videoVo = new VideoVo();
//                                    BeanUtils.copyProperties(video, videoVo);
//                                    videoVoList.add(videoVo);
//                                }
//                            }
//                chapterVo.setChildren(videoVoList);
//                list.add(chapterVo);
//
//            }
//        }ublic void removeMediasById(String id) {
//
//    }
//
//        return list;

    }


}
